﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Presentation.Module.Setup.Interfaces.ViewModels;
using Presentation.Module.Setup.Interfaces.Services;
using System.ComponentModel;
using Presentation.Module.Setup.Models;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using System.Collections.ObjectModel;
using Presentation.Module.Setup.Interfaces.Views;

namespace Presentation.Setup.Views
{
    public class EditSkillViewModel : IEditSkillViewModel, INotifyPropertyChanged
    {
        private Skill _CurrentSkill;
        private ObservableCollection<Task> _Tasks;

        public EditSkillViewModel(ISkillsService skillService, IAddTaskView addSkillView, IRegionManager regionManager)
        {
            this.SkillService = skillService;
            this.RegionManager = regionManager;

            RegionManager.Regions["MainRegion"].Add(addSkillView);

            this.AddTaskView = addSkillView;
            this.AddTaskView.ViewExit += new Shell.Interfaces.ViewExit(AddTaskView_ViewExit);

            this.Commit = new DelegateCommand(OnCommit);
            this.Cancel = new DelegateCommand(OnCancel);
            this.AddTask = new DelegateCommand(OnAddTask);
        }        

        void AddTaskView_ViewExit()
        {
            RegionManager.Regions["MainRegion"].Activate(this.View);
            Tasks = new ObservableCollection<Task>(SkillService.GetAllTasksForSkill(_CurrentSkill));
        }

        public IAddTaskView AddTaskView { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;

        public ICommand Commit { get; private set; }
        public ICommand Cancel { get; private set; }
        public ICommand AddTask { get; private set; }

        public Skill CurrentSkill
        {
            get
            {
                return _CurrentSkill;
            }
            set
            {
                if (value != null)
                {
                    _CurrentSkill = value;
                    RaiseOnPropertyChanged("CurrentSkill");
                    Tasks = new ObservableCollection<Task>(SkillService.GetAllTasksForSkill(_CurrentSkill));
                }
            }
        }
        public ObservableCollection<Task> Tasks
        {
            get
            {
                return _Tasks;
            }
            set
            {
                _Tasks = value;
                RaiseOnPropertyChanged("Tasks");
            }
        }

        public Module.Setup.Interfaces.Views.IEditSkillView View { get; set; }

        private ISkillsService SkillService { get; set; }
        private IRegionManager RegionManager { get; set; }

        private void OnCommit()
        {
            this.SkillService.EditSkill(_CurrentSkill);
            View.Exit();
        }

        private void OnCancel()
        {
            View.Exit();
        }

        private void OnAddTask()
        {
            AddTaskView.AddTaskToSkill(this.CurrentSkill);
            RegionManager.Regions["MainRegion"].Activate(AddTaskView);
        }

        private void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
